<template>
  <div>
    <section>
      <img src="../assets/login/login.jpg" class="bg" />
      <el-form :model="loginForm" :rules="rulesForm" ref="userForm_">
        <div class="login">
          <h2>Please Sign In</h2>
          <el-form-item class="inputBox"  prop="name" >
            <el-input type="text" v-model="loginForm.name" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item class="inputBox" prop="password" id="pass">
            <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" />
          </el-form-item>
          <div class="inputBox">
            <input type="button" @click="login" value="Login" id="btn" />
          </div>
          <div class="group">
            <a href="#">忘记密码</a>
            <a href="#">注册</a>
          </div>
        </div>
      </el-form>
      <div class="flower">
        <img src="../assets/login/11_1.png" />
        <img src="../assets/login/11_2.png" />
        <img src="../assets/login/11_3.png" />
        <img src="../assets/login/11_4.png" />
        <img src="../assets/login/11_5.png" />
        <img src="../assets/login/11_6.png" />
        <img src="../assets/login/11_4.png" />
        <img src="../assets/login/11_5.png" />
      </div>
    </section>
  </div>
</template>
<script>
import { login } from '../api/users'
export default ({
  name: "Login",
  data() {
    return {
      loginForm: {
        name: '明',
        password: '123abc'
      },
      rulesForm: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度为6-20位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.userForm_.validate((valid) => {
        if (valid) {
          //在这里进行springboot的ajax请求
          //校验账号密码，后台返回token
          login(this.loginForm).then(res => {
            console.log("发送了后端请求")
            if (res.data.code === 0) {
              this.$message({
                type: 'error',
                message: res.data.msg
              })
              return
            }
            //保存token
            localStorage.setItem('userToken', res.data.data)
            //登录成功，跳转home
            this.$router.push({
              path: '/'
            }, () => { }, () => { })
          }).catch(error => {
            this.$message({
              type: 'error',
              message: '请求接口出错'
            })
          })

        }
      })
    }
  }
})
</script>
<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

section {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow-x: hidden;
}

/* 图片铺满屏幕 */
section .bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 登录容器的基本架子 */
.login {
  position: relative;
  width: 500px;
  min-height: 300px;
  padding: 60px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(3px);
  display: flex;
  flex-direction: column;
  gap: 20px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.2);
}

/* 设置  Please Sign In标题 的样式 */
.login h2 {
  position: relative;
  width: 100%;
  text-align: center;
  font-size: 2.5em;
  font-weight: 600;
  color: #8f2c24;
  margin-bottom: 10px;
}

/* 设置输入框的样式 */
.login .inputBox {
  position: relative;
}

.login .inputBox input {
  position: relative;
  width: 100%;
  padding: 15px 20px;
  outline: none;
  font-size: 1.25em;
  color: #8f2c24;
  border-radius: 5px;
  background: #fff;
  border: none;
  margin-bottom: 30px;

}

/* 提示词和按钮的样式 */
.login .inputBox ::placeholder {
  color: #8f2c24;
}

.login .inputBox #btn {
  position: relative;
  border: none;
  outline: none;
  background: #8f2c24;
  color: #fff;
  cursor: pointer;
  font-size: 1.25em;
  font-weight: 500;
}

/* 忘记密码的样式 */
.login .group {
  display: flex;
  justify-content: space-between;
}

.login .group a {
  font-size: 1.25em;
  color: #8f2c24;
  font-weight: 500;
  text-decoration: none;
}

/* 大容器铺满屏幕，小花朵定位 */
.flower {
  position: absolute;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  z-index: 1;
  pointer-events: none;
}

.flower img {
  position: absolute;
  width: 2%;
}

/* 花朵飘落 */
@keyframes animate {
  0% {
    opacity: 0;
    top: -10px;
    transform: translateX(20px) rotate(0deg);
  }

  10% {
    opacity: 1;
  }

  20% {
    transform: translateX(-20px) rotate(45deg);
  }

  40% {
    transform: translateX(-20px) rotate(90deg);
  }

  60% {
    transform: translateX(20px) rotate(180deg);
  }

  80% {
    transform: translateX(-20px) rotate(45deg);
  }

  100% {
    top: 110%;
    transform: translateX(20px) rotate(225deg);
  }
}

/* 花朵飘落动画 */
.flower img:nth-child(1) {
  left: 20%;
  animation: animate 20s linear infinite;
}

.flower img:nth-child(2) {
  left: 50%;
  animation: animate 14s -2s linear infinite;
}

.flower img:nth-child(3) {
  left: 70%;
  animation: animate 12s -3s linear infinite;
}

.flower img:nth-child(4) {
  left: 5%;
  animation: animate 15s -2s linear infinite;
}

.flower img:nth-child(5) {
  left: 85%;
  animation: animate 18s -1s linear infinite;
}

.flower img:nth-child(6) {
  left: 90%;
  animation: animate 12s -1s linear infinite;
}

.flower img:nth-child(7) {
  left: 15%;
  animation: animate 14s -2s linear infinite;
}

.flower img:nth-child(8) {
  left: 60%;
  animation: animate 15s -1s linear infinite;
}
</style>